package vip.xiaonuo.smzq.modular.psba.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaSqjl;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaSqjlVo;

import java.util.Map;

/**
 * 评审备案申请记录表 Mapper
 *
 * @author Kevin
 * @since 2024-06-11
 */
@Mapper
public interface PsbaSqjlMapper extends BaseMapper<PsbaSqjl> {

    /**
     * 评审备案申请审批表分页查询
     *
     * @param page
     * @param queryWrapper
     * @author Kevin
     * @since 2024/7/18 15:01
     */
    @Select("with a as (\n" +
            "SELECT c.pkid, c.psbabh, MAX(g.czlx) czlx\n" +
            "FROM psba_sqjl c\n" +
            "LEFT JOIN psba_sqjl_czrz g ON c.psbabh = g.psbajl_id\n" +
            "GROUP BY c.pkid, c.psbabh),\n" +
            "b as (\n" +
            "select a.pkid, a.psbabh, a.czlx, (CAST(a.czlx AS INT) + 1) as next_num, z.czsj, z.czrxm\n" +
            "from a left join psba_sqjl_czrz z on a.psbabh = z.psbajl_id and a.czlx = z.czlx)\n" +
            "select p.*, b.czlx, b.next_num, b.czsj, b.czrxm,\n" +
            "w.wjmc as bgwjmc, w.wjlj\n" +
            "from psba_sqjl p\n" +
            "left join b on p.pkid = b.pkid\n" +
            "left join psba_gt_wjglb w on p.bgwj = w.pkid\n" +
            "${ew.customSqlSegment}")
    Page<PsbaSqjlVo> psbaSqjlPage(IPage<PsbaSqjlVo> page, @Param(Constants.WRAPPER) Wrapper<Map> queryWrapper);
}
